package com.aishua.andpaydemo.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.aishua.andpaydemo.Constant;
import com.aishua.andpaydemo.R;
import com.aishua.andpaydemo.fragment.BaseFragment;
import com.aishua.andpaydemo.fragment.DiscoverFragment;
import com.aishua.andpaydemo.fragment.FoundFragment;
import com.aishua.andpaydemo.fragment.HomeFragment;
import com.aishua.andpaydemo.fragment.MineFragment;
import com.aishua.andpaydemo.fragment.ShopFragment;

import java.util.ArrayList;
import java.util.IllegalFormatCodePointException;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity-";
    private RadioGroup rg_main;
    private ArrayList<BaseFragment> baseFragments;
    private int position;
    private RadioButton rb_found;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initFragment(0, "HOME_FRAGMET");
    }

    private void initData() {

        baseFragments = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        baseFragments.add(homeFragment);
        baseFragments.add(new FoundFragment());
        baseFragments.add(new DiscoverFragment());
        baseFragments.add(new ShopFragment());
        baseFragments.add(new MineFragment());
        homeFragment.setCheckPositionListener(new HomeFragment.CheckPositionListener() {

            @Override
            public void changePosition(int positon) {

                if (positon == 7 || positon == 6) {

                    initFragment(1, "FOUND_FRAGMET");
                    rb_found.setChecked(true);
                }

                if (positon == 7) {

                    Constant.isThrid = true;
                }

            }
        });

    }

    private void initView() {

        rg_main = findViewById(R.id.rg_main);
        rg_main.setOnCheckedChangeListener(new MyCheckedChangeListener());
        rb_found = findViewById(R.id.rb_found);
    }


    public class MyCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            String tag = null;
            switch (checkedId) {

                case R.id.rb_home:

                    position = 0;

                    tag = "HOME_FRAGMET";
                    break;
                case R.id.rb_found:

                    position = 1;
                    tag = "FOUND_FRAGMET";
                    break;
                case R.id.rb_discover:
                    position = 2;
                    tag = "DISCOVER_FRAGMET";
                    break;
                case R.id.rb_shop:
                    position = 3;
                    tag = "SHOP_FRAGMENT";
                    break;
                case R.id.rb_myhouse:

                    position = 4;
                    tag = "MYHOUSE_FRAGMENT";
                    break;
            }
            initFragment(position, tag);
        }
    }

    private void initFragment(int position, String tag) {
        //1.得到FragmentManger
        FragmentManager fm = getSupportFragmentManager();
        //2.开启事务
        FragmentTransaction ft = fm.beginTransaction();
        //3.替换
        ft.replace(R.id.fl_main, baseFragments.get(position), tag);//主页
        //4.提交
        ft.commit();
    }
    long startTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            long currentTime = System.currentTimeMillis();
            if ((currentTime - startTime) >= 2000) {
                Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
                startTime = currentTime;
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


}
